package com.vcredit.creditcat.service.income;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.BUIGridResultDto;
import com.vcredit.creditcat.dto.income.IncomeForPcDto;
import com.vcredit.creditcat.dto.income.IncomeSearchParamDto;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.IncomeType;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.RecommendCard;
import com.vcredit.creditcat.model.ext.IncomeForPcExt;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.MerchantProductService;
import com.vcredit.creditcat.service.RecommendCardService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.util.BeanUtil;

@Service
public class IncomeForPcService {
	
	@Autowired
	private MyIncomeService myIncomeService;
	
	@Autowired
	private CustAccountService custAccountService;
	
	@Autowired
	private IncomeTypeService incomeTypeService;
	
	@Autowired
	private IncomeLogService incomeLogService;
	
	@Autowired
	private MerchantProductService merchantProductService;
	
	@Autowired
	private RecommendCardService recommendCardService;
	
	public Boolean add(IncomeForPcDto param) {
		try {
			CustAccount custAccount = new CustAccount(null, null, param.getUserName(), null, null);
			List<CustAccount> list = custAccountService.find(custAccount);
			if(Utility.isNullOrEmpty(list))
				return false;
			Long custId = list.get(0).getCustId();
			
			IncomeType incomeType = incomeTypeService.findById(param.getIncomeTypeId());
			if(null == incomeType) 
				return false;
			
			return myIncomeService.addIncome(custId, param.getIncomeTypeId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<IncomeForPcDto> incomeTypeList() {
		List<IncomeForPcDto> result = new ArrayList<IncomeForPcDto>();
		
		List<IncomeType> allIncomeType = incomeTypeService.findAll();
		Map<Long, RecommendCard> allCardMap = recommendCardService.findAllForMap();
		Map<Long, MerchantProduct> allProductMap = merchantProductService.findAll();
		
		List<IncomeForPcDto> productIncomeList = new ArrayList<IncomeForPcDto>();
		List<IncomeForPcDto> cardIncomeList = new ArrayList<IncomeForPcDto>();
		
		for (IncomeType incomeType : allIncomeType) {
			IncomeForPcDto dto = new IncomeForPcDto();
			dto.setIncomeTypeId(incomeType.getIncomeId());
			if(incomeType.getIncomeType() == 0) {
				dto.setIncomeFromName(allProductMap.get(incomeType.getProductCardId()).getProductName());
				productIncomeList.add(dto);
			}
			else {
				dto.setIncomeFromName(allCardMap.get(incomeType.getProductCardId()).getBank());
				cardIncomeList.add(dto);
			}
		}
		result.addAll(productIncomeList);
		result.addAll(cardIncomeList);
		return result;
	}

	
	public BUIGridResultDto<IncomeForPcDto> pageQuery(IncomeSearchParamDto searchParamDto) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("incomeType", searchParamDto.getIncomeType());
		param.put("userName", searchParamDto.getUserName());
		param.put("startTime", searchParamDto.getStartTime());
		param.put("endTime", searchParamDto.getEndTime());
		param.put("start", searchParamDto.getStart());
		param.put("limit", searchParamDto.getLimit());
		List<IncomeForPcExt> list = incomeLogService.pageQueryForPc(param);
		List<IncomeForPcDto> rows = new ArrayList<IncomeForPcDto>();
		for (IncomeForPcExt incomeForPcExt : list) {
			incomeForPcExt.setIncomeFromName(this.getProductOrCardName(incomeForPcExt.getIncomeType(), 
					incomeForPcExt.getIncomeFromId()));
			
			IncomeForPcDto dto = new IncomeForPcDto();
			BeanUtil.copy(incomeForPcExt, dto);
			rows.add(dto);
		}
		long count = incomeLogService.queryCountForPc(param);
		return new BUIGridResultDto<IncomeForPcDto>(count, rows);
	}

	public String getProductOrCardName(int incomeType, long productCardId) {
		if(incomeType == 0) { // 贷款
			return merchantProductService.findAll().get(productCardId).getProductName();
		} else { // 办卡
			return recommendCardService.findAllForMap().get(productCardId).getBank();
		}
	}
}
